package com.uit.ecommerce.servlets;

import com.uit.ecommerce.bo.CityBO;
import com.uit.ecommerce.bo.WeatherCityBO;
import com.uit.ecommerce.dto.CityDTO;
import com.uit.ecommerce.dto.WeatherCityDTO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ngloc_it
 */
@WebServlet(name="DetailCity", urlPatterns={"/DetailCity"})
public class DetailCity extends HttpServlet {
   
       // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);

        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        HttpSession session = req.getSession();
        PrintWriter out = resp.getWriter();

        String function = (String)req.getParameter("function");
        String cityName = (String) req.getParameter("city");
        session.setAttribute("cityname", cityName);

        if(function.equalsIgnoreCase("submit")){
            try {
                submitWeather(req, resp);                
            } catch (Exception ex) {
                Logger.getLogger(DetailCity.class.getName()).log(Level.SEVERE, null, ex);
            }
            return;
        }
        String path = "./jsps/ChiTietThoiTietTinhThanh.jsp";
        resp.sendRedirect(path);
    }

    private void submitWeather(HttpServletRequest req, HttpServletResponse resp) throws IOException, Exception{
        String report = "Cập nhật thành công";
        PrintWriter out = resp.getWriter();
        WeatherCityBO wcB = new WeatherCityBO();
        String cityName = (String) req.getParameter("city");                
        String date = (String) req.getParameter("date");
        String tempMin = (String) req.getParameter("tempMin");
        String tempMax = (String) req.getParameter("tempMax");
        String info = (String) req.getParameter("info");
        String status = (String) req.getParameter("status");

        CityBO cityBo = new CityBO();
        CityDTO cityDto = cityBo.getCityByName(cityName);
        String cityId = cityDto.getId();
        WeatherCityDTO wcd = new WeatherCityDTO(cityId, cityName, date, Float.parseFloat(tempMax), Float.parseFloat(tempMin), info, status);
        WeatherCityBO wcb = new WeatherCityBO();
        if(wcb.checkWeatherCityExit(wcd) == true)
            report = "Thời tiết cho tỉnh/TP " + cityDto.getName() + " ngày " + date + "đã có";
        else
            wcb.insertWeatherCity(wcd);
        
        out.println(report);
    }
}
